package cn.zjxiliu.lib_login.ui.register;

import android.content.Intent;
import android.text.TextUtils;

import cn.zjxiliu.lib_login.LoginSDK;
import cn.zjxiliu.lib_login.R;
import cn.zjxiliu.lib_login.server.HttpManager;
import cn.zjxiliu.lib_login.server.callback.CheckPhoneHasRegisterCallback;
import cn.zjxiliu.lib_login.server.callback.GetVerificationCodeCallback;
import cn.zjxiliu.lib_login.server.callback.LoginCallback;
import cn.zjxiliu.lib_login.server.request.CheckPhoneHasRegisterRequest;
import cn.zjxiliu.lib_login.server.request.RegisterRequest;
import cn.zjxiliu.lib_login.server.request.VerificationCodeRequest;
import cn.zjxiliu.lib_login.server.response.LoginResponse;
import cn.zjxiliu.lib_login.ui.base.AbsBaseViewHelper;

/**
 * Created by jessie on 2018/11/21.
 */

public class RegisterViewHelper extends AbsBaseViewHelper {
    public static final int REGISTER_STEP_ONE = 0;
    public static final int REGISTER_STEP_TWO = 1;
    public static final int REGISTER_STEP_THREE = 2;


    private IRegisterView registerView;

    private int currentStep;


    public RegisterViewHelper(IRegisterView registerView) {
        this.registerView = registerView;
    }


    @Override
    public void initHelper() {
        currentStep = REGISTER_STEP_ONE;
        if(getLoginConfig().isUsePad()){
            registerView.isScreenLandscape(true);
            registerView.initView(R.layout.activity_register_pad);
        }else{
            registerView.isScreenLandscape(false);
            registerView.initView(R.layout.activity_register);
        }
    }

    public int getCurrentStep() {
        return currentStep;
    }

    public void setCurrentStep(int view) {
        currentStep = view;
    }


    /**
     * 获取验证码
     */
    public void getVerificationCodeWhenRegister(final boolean isNeedToStep2) {
        registerView.showLoadingDialog();
        HttpManager.getInstance().getVerificationCode(new VerificationCodeRequest(), new GetVerificationCodeCallback() {
            @Override
            public void onGetVerificationCodeSuccess() {
                if(registerView != null){
                    registerView.hideLoadingDialog();
                    registerView.onVerificationCodeStart();
                }
            }

            @Override
            public void onOperateFailed(String msg) {
                if(registerView != null){
                    registerView.hideLoadingDialog();
                    registerView.showToastMsg(msg);
                }
            }
        });
    }

    /**
     * 跳转到配置文件中所配置的界面
     * @param viewShowWhenLoginFinished
     */
    public void openViewByConfigView(String viewShowWhenLoginFinished) {
        try {
            Intent intent = new Intent(registerView.getCurrentActivity(), Class.forName(viewShowWhenLoginFinished));
            registerView.getCurrentActivity().startActivity(intent);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();

        }
    }

    public void startRegisterAndLogin() {
        registerView.showLoadingDialog();
        HttpManager.getInstance().startRegister(new RegisterRequest(), new LoginCallback() {
            @Override
            public void onLoginSuccess(LoginResponse loginResponse) {
                if(registerView != null){
                    if(getLoginConfig().isFinishedWhenRegisterSuccess()){
                        if(LoginSDK.getInstance().getListener() != null){
                            LoginSDK.getInstance().getListener().onLoginSuccess(loginResponse);
                        }
                        if(!TextUtils.isEmpty(getLoginConfig().getViewShowWhenLoginFinished())){
                            openViewByConfigView(getLoginConfig().getViewShowWhenLoginFinished());
                        }
                        registerView.finishAllViews();
                    }else{
                        registerView.finishSelf();
                    }
                }
            }

            @Override
            public void onOperateFailed(String msg) {
                if(registerView != null){
                    registerView.hideLoadingDialog();
                    registerView.showToastMsg(msg);
                }
            }
        });

    }


    public void checkPhoneHasRegisterAndGotoStep2() {

        HttpManager.getInstance().checkPhoneHasRegister(new CheckPhoneHasRegisterRequest(), new CheckPhoneHasRegisterCallback() {
            @Override
            public void onPhoneHasRegisterSuccess(boolean isRegister) {

            }

            @Override
            public void onOperateFailed(String msg) {

            }
        });
    }

    public void checkVerificationCodeIsRightAndGoToStep3() {
    }
}
